package com.gzone.contentprovider.sql.dao;

import java.util.ArrayList;

import android.database.Cursor;

import com.gzone.contentprovider.helper.DataHelper;
import com.gzone.contentprovider.sql.model.Hit;

public class HitDAO implements DAO<Hit>{
	private static final String TABLE_NAME ="Hit";
	private static final String ID ="Hit_ID";
	private static final String ITEM_ID ="Hit_item_ID";
	private static final String DATE ="Hit_date";
	private static final String CODE ="Hit_code";
	private static final String IDENT ="Hit_ident";
	private static final int CODE_DISPLAY  = 1;
	private static final int CODE_LIKE  = 2;
	private static final int CODE_SELECTION  = 3;
	
	public HitDAO(){
		DataHelper.dropTable(TABLE_NAME);
		String tableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
		+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
		+ ITEM_ID + " INTEGER," 
		+ DATE + " TEXT," 
		+ CODE + " INTEGER," 
		+ IDENT + " TEXT"
		+");";
		DataHelper.createTable(tableSql);
	}
	@Override
	public void delete(Hit object) {
		// TODO Auto-generated method stub
		DataHelper.delete(TABLE_NAME, object.id);
	}
	
	@Override
	public void deleteAllRow() {
		DataHelper.deleteAllRow(TABLE_NAME);
	}

	@Override
	public ArrayList<Hit> findAll() {
		// TODO Auto-generated method stub
		Cursor data = DataHelper.readAll(TABLE_NAME);
		ArrayList<Hit> list = new ArrayList<Hit>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int date = data.getColumnIndex(DATE);
			int code = data.getColumnIndex(CODE);
			int ident = data.getColumnIndex(IDENT);
			do {
				Hit obj = new Hit();
				obj.id = data.getInt(id);
				obj.item_id = data.getInt(item_id);
				obj.date = data.getString(date);
				obj.code = data.getInt(code);
				obj.ident = data.getString(ident);
				list.add(obj);
			} while (data.moveToNext());
		}
		return list;
	}

	@Override
	public Hit findById(int id) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, ID, id);
		Hit obj = null;
		if (data.getCount() > 0) {
			data.moveToFirst();
			int hid = data.getColumnIndex(ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int date = data.getColumnIndex(DATE);
			int code = data.getColumnIndex(CODE);
			int ident = data.getColumnIndex(IDENT);
			do {
				obj = new Hit();
				obj.id = data.getInt(hid);
				obj.item_id = data.getInt(item_id);
				obj.date = data.getString(date);
				obj.code = data.getInt(code);
				obj.ident = data.getString(ident);
			} while (data.moveToNext());
		}
		return obj;
	}

	@Override
	public ArrayList<Hit> findByParameter(String paraName, Object value) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, paraName, value);
		ArrayList<Hit> list = new ArrayList<Hit>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int item_id = data.getColumnIndex(ITEM_ID);
			int date = data.getColumnIndex(DATE);
			int code = data.getColumnIndex(CODE);
			int ident = data.getColumnIndex(IDENT);
			do {
				Hit obj = new Hit();
				obj.id = data.getInt(id);
				obj.item_id = data.getInt(item_id);
				obj.date = data.getString(date);
				obj.code = data.getInt(code);
				obj.ident = data.getString(ident);
				list.add(obj);
			} while (data.moveToNext());
		}
		return list;
	}

	@Override
	public void insert(Hit object) {
		String insert = "INSERT INTO '" + TABLE_NAME + "'("
		+ ITEM_ID + "," 
		+ DATE + "," 
		+ CODE + "," 
		+ IDENT + ""
		+") VALUES(?,?,?,?);";
		Object[] bindArgs = {object.item_id, object.date, object.code, object.ident};
		DataHelper.insert(insert, bindArgs );
	}

	@Override
	public void update(Hit object) {
	}
	@Override
	public ArrayList<Hit> findAllToUpdate() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ArrayList<Hit> readJSONData(String data) {
		// TODO Auto-generated method stub
		return null;
	}

}
